//
//  SkinViewController.m
//  ErWa
//
//  Created by 王阳阳 on 16/3/15.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SkinViewController.h"
#import "SkinCollectionViewCell.h"
@interface SkinViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArr;//数组
//@property (nonatomic, strong) SkinCollectionViewCell *cell;
@property (nonatomic, strong)NSMutableArray *newstr;
@property (nonatomic, assign)NSInteger bc ;
@property (nonatomic ,assign) NSInteger i;
@end

@implementation SkinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatCollectionView];//创建CollectionView
    //图片名字数组
    _dataArr = [@[@"12.jpg",@"30.jpeg",@"29.jpeg",@"13.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg"] mutableCopy];
}
- (void)creatCollectionView
{
    
    
    //布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width  - 40)/3, 150);//item大小
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //创建CollectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor clearColor];//清空背景颜色
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    //重用池
    [_collectionView registerClass:[SkinCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
  
}
#pragma mark - collectionView必须实现的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   SkinCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.tag = 1000+indexPath.item;//给cell附tag值
    cell.biaoshi.image = [UIImage imageNamed:@"cuo"];
    cell.img.image = [UIImage imageNamed:_dataArr[indexPath.item]];
    return cell;
}
#pragma mark - collectionView
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.str = _dataArr[indexPath.item];
    
   //图片数组
    //归档 获取地址
    NSString *path =   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    //2.拼接地址 (按照什么路径 保存为什么文件) (数组和字典写plist)
    NSString *strpath = [path stringByAppendingPathComponent:@"a.plist"];
    [NSKeyedArchiver archiveRootObject:self.str toFile:strpath];
    //解档
    NSString *new  =[NSKeyedUnarchiver unarchiveObjectWithFile:strpath];
    self.view.backgroundColor = [UIColor clearColor];//清空背景颜色
    UIColor *imagee = [UIColor colorWithPatternImage:[UIImage imageNamed:new]];
    [self.view setBackgroundColor:imagee];//添加背景图片

    //遍历i
    for (NSInteger i = 1000; i < 1011; i++) {
        SkinCollectionViewCell *cell = [self.view viewWithTag:i];
        if (i == indexPath.item + 1000) {
            cell.biaoshi.image = [UIImage imageNamed:@"dui"];
        }else {
            cell.biaoshi.image = [UIImage imageNamed:@"cuo"];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
